<?php
/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2018/8/2
 * Time: 上午10:12
 */

namespace app\portal\controller;


use app\portal\model\DownloadModel;
use app\portal\model\DownloadUserModel;
use cmf\controller\AdminBaseController;

class AdminDownloadUserController extends AdminBaseController
{


    public function index()
    {
        $title = $this->request->request('keyword', '');
        $where = [];
        if ($title) {
            $where['username'] = ['like', "%$title%"];
        }
        $downloads = DownloadUserModel::where($where)->paginate(15);
        $this->assign('downloads', $downloads->items());
        $this->assign('page', $downloads->render());
        $this->assign('keyword', $title);
        return $this->fetch();
    }

    public function delete($id){
        if($id){
            DownloadUserModel::destroy($id);
        }
        $this->success('删除成功');
    }

    public function add()
    {
        return $this->fetch();
    }


    public function addPost()
    {
        $post = $this->request->request('post/a');

        $model = new DownloadUserModel();

        $post['create_time'] = time();

        $insert = $model->save($post);
        if ($insert) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }

    public function edit()
    {
        $id = $this->request->request('id');
        $model = new DownloadUserModel();
        $download = $model->find($id);
        $this->assign('post', $download);
        return $this->fetch();
    }


    public function editPost()
    {
        $id = $this->request->request('id');

        $post = $this->request->request('post/a');
//        $file_url = $this->request->request('file_urls/a');
//        $file_names = $this->request->request('file_names/a');
//        if (count($file_url) > 1) {
//            $this->error('只能上传一个附件');
//        } else if (count($file_url) == 0) {
//            $this->error('请上传附件');
//        }
        $model = new DownloadUserModel();
        $download = $model->find($id);
//        $post['file'] = $file_url[0];
//        $post['file_name'] = $file_names[0];
        $insert = $download->save($post);
        if ($insert) {
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }


}